//查询进货单
function queryJH(){
	var configStr = parseConfig('QUERYCLN');
	$('#queryConfig').html(parseConfigStr('QUERYCLN'));
	$("#"+tabDiv).tabs('select',_bill_title+'列表');
	//加载shuju
	//$('#DATALIST').datagrid({url:'../../framework/loadData?dsId='+datastoreId+"&config="+configStr});
	$.ajax({
		url:'../../framework/loadData',
		data:{dsId:_ds_id,config:configStr},
		success:function(queryJson){
			$('#DATALIST').datagrid("loadData", queryJson);
			peList = queryJson.rows;
			$('#DATALIST').datagrid({url:queryJson.url});
		}
	});
}

var _aOrm = 0;
//添加页面 显示商品窗口
function addProduct(){
	showProductWin('addWinForm');
}

//修改页面 显示商品窗口
function addProductM(){
	showProductWin('modWinForm');
}

//添加到货单(主明细窗口)
function addObj(){
	_aOrm = 0;
	$('#addWin').window('open');
	if($('#addWinTable').html()==''){
		//加载主表字段 和 明细Datagrid
		//加载主表按钮
		$.ajax({
			type:'post',
			url:'../../framework/buttonsListC',
			data:{'butnId':"fxJhA"},
			success:function(data){
				if(data == ""){
					alert("按钮数据为空");
				}else{
					var buttons = data;
					var butHTML = "";
					for(var i = 0; i < buttons.length; i++){
						butHTML += formatButtons(buttons[i]);
					}
					$('#addWinBut').html(butHTML);
					$.parser.parse('#addWinBut');  //重新编译界面
				}
			}
		});
		//加载明细按钮
		$.ajax({
			type:'post',
			url:'../../framework/buttonsListC',
			data:{'butnId':"fxJhItemA"},
			success:function(data){
				if(data == ""){
					alert("按钮数据为空");
				}else{
					var buttons = data;
					var butHTML = "";
					for(var i = 0; i < buttons.length; i++){
						butHTML += formatButtons(buttons[i]);
					}
					$('#addItemBut').html(butHTML);
					$.parser.parse('#addItemBut');  //重新编译界面
				}
			}
		});
		//加载主表字段
//		divName='addWinTable';//条件字段
//		makeColumns(clons,divName);
		
		$.ajax({
            type:'post',
            url:'../../framework/loadColumnsU',
            data:{'colId':_ds_id,'type':'1'},
            success:function(data){
                
                if(data ==""){
                    alert("数据为空");
                }else{
                    //请求一个组织列的方法
                    producePage(data,'addWinTable','1','');
                }
            }
         });
		
		//加载Datagrid按钮
		loadItemDategrid();
		//初始化商品窗口
		initProductWin();
	}
}

//加载明细数据表格框
function loadItemDategrid(){
	
//	$.ajax({
//		type:'post',
//		url:'../../framework/listQueryJson',
//		data:{'dsId':_ds_item_id,'colId':_ds_item_id,'execFlag':''},
//		success:function(data){
//			if(data==""){
//				alert("数据为空");
//			}else{
//				//给datagrid赋列值，没有数据
//				mItemDatagrid(data,'addItemDatagrid');
//			}
//		}
//	});
	

	$.ajax({
        type:'post',
        url:'../../framework/loadColumnsU',
        data:{'colId':_ds_item_id,'type':'4'},
        success:function(data){
            
            if(data ==""){
                alert("数据为空");
            }else{
                //请求一个组织列的方法
                producePage(data,'addItemDatagrid','4','');
            }
        }
     });
	
	
	
	
}

//显示添加商品窗口
function showProductWin(divName){
	//检查是否选择了 ：承接厂
	//判断添加明细的条件
//	if($('#'+divName+' select[name=jobManName]').val() == null || $('#'+divName+' select[name=jobManName]').val() == '' || ($('#'+divName+' select[name=jobManName]').val() == '00')){
		//$.messager.alert('提示框',"请先选择承接厂",'info');
		//如果没选，则不做任何操作
		//return;
//	}
	//加载商品信息
	if($('#productDatagrid').datagrid('getRows').length == 0){
		$.ajax({
			url:'../../framework/loadData',
			data:{dsId:'bs_product',config:''},//config:'JOB_MAN_NAME-'+$('#addWinForm select[name=jobManName]').val()
			success:function(queryJson){
				$('#productDatagrid').datagrid("loadData", queryJson);
				peList = queryJson.rows;
				$('#productDatagrid').datagrid({url:queryJson.url});
			}
		});
	}
	//打开商品窗口
	$("#productWin").window('open');
}


//***********初始化商品窗口
function initProductWin(){
	if ( $("#productWin").length > 0 ) {
		$("#productWin").window('close');
	}else{
		$('#addWin').append("<div id='productWin'><div id='product'><div id='productBut' style='padding: 10px;'></div><div id='productDatagrid'></div></div></div>");
		$('#productWin').window({  
		    width:document.documentElement.clientWidth,  
		    height:document.documentElement.clientHeight,  
		    title:'添加商品信息',
		    resizable:false,
		    maximizable:false,
		    minimizable:false
		}); 
		$('#productWin').window('close');
		//加载商品列表字段
		lPDatagrid();
		//加载按钮
		$.ajax({
			type:'post',
			url:'../../framework/buttonsListC',
			data:{'butnId':"yzSprkProductA"},
			success:function(data){
				if(data == ""){
					alert("按钮数据为空");
				}else{
					var buttons = data;
					var butHTML = "";
					for(var i = 0; i < buttons.length; i++){
						butHTML += formatButtons(buttons[i]);
					}
					$('#productBut').html(butHTML);
					$.parser.parse('#productBut');  //重新编译界面
				}
			}
		});
	}
}

//确定添加  将商品的Datagrid 复制到  到货的Datagrid
function sureAP(){
	var divName = "addItemDatagrid";
	if(_aOrm == 0){
		divName = "addItemDatagrid";
	}else if(_aOrm == 1){
		divName = "modItemDatagrid";
	}
	
	$('#productWin').window('close');
	//选择的商品
	var selRows = $('#productDatagrid').datagrid('getSelections');
	//原到货明细
	var oldRows = $('#'+divName).datagrid('getRows');
	var selParRow = '';
	var ff = 0;
	for(var i = 0; i < selRows.length; i++){
		ff = 0;
		
		for(var j = 0; j < oldRows.length; j++){
			if(oldRows[j].SP_ID ==  selRows[i].H_ID){//商品ID相同，为同一个商品
				//var mas = flowNodeFormatter(oldRows[j].FLOW_ID);
				oldRows[j].SP_AMOUNT = oldRows[i].SP_AMOUNT + 1;
				$('#'+divName).datagrid('refreshRow', j);
				ff = 1;
				break;
			}
		}
		if(ff == 1)continue;
		$('#'+divName).datagrid('appendRow',{
			JH_ID:'',
			JH_ITEM_ID:'',
			STOCK_ID:'',
			SP_ID:selRows[i].H_ID,
			SP_UNIT:'',
			SP_AMOUNT:'',
			SP_REAL_PRICE:'',
			TAX_RATE:'',
			TAX_MONEY:'',
			REAL_MONEY:selRows[i].H_PRICE+'',
			SP_DISCOUNT:'',
			SOURCE_ID:'',
			SP_PRICE:selRows[i].H_PRICE+'',
			MEMO:'',
			REAL_MONEY_ITEM:'',
			TOTAL_MONEY_ITME:'',
			SP_AMONUT_MISTAKE:'',
			H_TYPE:''
		});
	}
}



//保存主表和明细
function saveJH(){
	var divName = "addItemDatagrid";
	var formId = "addWinForm";
	if(_aOrm == 0){
		divName = "addItemDatagrid";
		formId = "addWinForm";
	}else if(_aOrm == 1){
		divName = "modItemDatagrid";
		formId = "modWinForm";
	}
	$('#'+divName).datagrid('acceptChanges');
	if(!(checkRequire(formId)=='Success')){
		return;
	}
	var rowsData = $('#'+divName).datagrid('getRows');
	if(!(rowsData.length > 0)){
		$.messager.alert("提示框","请添加"+_bill_title+"明细");
		return;
	}
	var jhObj = {};
	jhObj.fxJhItems = formatToJsonJh(rowsData);
	jhObj.fxJh = getFormJson(formId);
	$.ajax({
		url:"../../jh/saveJh",
		type:'post',
		contentType:"application/json;charset=utf-8",
		data:JSON.stringify(jhObj),
		dataType:"json",
		success:function(data){
			if(data.msg == 'success'){
				$.messager.alert("提示框","保存成功");
				//将主键返回
				$('#'+formId+' input[name=jhId]').val(data.info);
				//重新加载明细数据
				$.ajax({
					url:'../../framework/loadData',
					data:{dsId:_ds_item_id,config:'JH_ID-'+$('#'+formId+' input[name=jhId]').val()},//config:'JOB_MAN_NAME-'+$('#addWinForm select[name=jobManName]').val()
					success:function(queryJson){
						$('#'+divName).datagrid("loadData", queryJson);
						peList = queryJson.rows;
						$('#'+divName).datagrid({url:queryJson.url});
					}
				});
			}else if(data.msg == 'upsuccess'){
				$.messager.alert("提示框","更新成功");
			}else if(data.msg == 'fail'){
				$.messager.alert("提示框","保存失败","info");
			}else if(data.msg == 'upfail'){
				$.messager.alert("提示框","更新失败","info");
			}else{
				$.messager.alert("错误提示框",data.msg,"error");
			}
		}
	});
}


//到货明细对象转换Json
function formatToJsonJh(rowsData){
	var kk = new Array();
	for(var k = 0; k < rowsData.length; k++){
		kk[k] = {
				jhId:rowsData[k].JH_ID,
				jhItemId:rowsData[k].JH_ITEM_ID,
				stockId:rowsData[k].STOCK_ID,
				spId:rowsData[k].SP_ID,
				spUnit:rowsData[k].SP_UNIT,
				spAmount:rowsData[k].SP_AMOUNT,
				spRealPrice:rowsData[k].SP_REAL_PRICE,
				taxRate:rowsData[k].TAX_RATE,
				taxMoney:rowsData[k].TAX_MONEY,
				realMoney:rowsData[k].REAL_MONEY,
				spDiscount:rowsData[k].SP_DISCOUNT,
				sourceId:rowsData[k].SOURCE_ID,
				spPrice:rowsData[k].SP_PRICE,
				memo:rowsData[k].MEMO,
				realMoneyItem:rowsData[k].REAL_MONEY_ITEM,
				totalMoneyItme:rowsData[k].TOTAL_MONEY_ITME,
				spAmountMistake:rowsData[k].SP_AMOUNT_MISTAKE,
				htype:rowsData[k].H_TYPE
		};
	}
	return kk;
}

//双击显示详情页面
//双击打开详情
function showInfo(rowData){
	var divName = "infoWinForm";
	//填充主表信息
	fillInfo(divName,rowData);
	//将主表变为不可写
	readOnlyAll(divName);
	var configStr = "JH_ID-"+rowData.JH_ID;
	//加载Data数据
	$.ajax({
		url:'../../framework/loadData',
		data:{dsId:_ds_item_id,config:configStr},
		success:function(queryJson){
			$('#infoItemDatagrid').datagrid("loadData", queryJson);
			$('#infoItemDatagrid').datagrid({url:queryJson.url});
		}
	});
	$('#infoWin').window('open');
}



//修改按钮
function modObj(){
	_aOrm = 1;
	var configStr = "JH_ID-"+$('#infoWinForm input[coluname=JH_ID]').val();
	//填充数据
	var divName = "modWinForm";
	//Ajax填充主表信息(根据主表Id)
	
	$.ajax({
		url:'../../framework/loadRowData',
		data:{dsId:_ds_id,config:configStr},
		success:function(rowData){
			fillInfo(divName,rowData);
			//将部分变成不可写
			readOnlyPart(divName);
		}
	});
	
	
	
	//加载Data数据
	$.ajax({
		url:'../../framework/loadData',
		data:{dsId:_ds_item_id,config:configStr},
		success:function(queryJson){
			$('#modItemDatagrid').datagrid("loadData", queryJson);
			$('#modItemDatagrid').datagrid({url:queryJson.url});
		}
	});
	
	
	$('#modWin').window('open');
	//初始化商品窗口
	initProductWin();
}






